#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# @(#)12   1.9         src/rsct/cfg_access/scaffold/ct_init_node.sh, cfg.access, rsct_rzauh, rzauh0431a 4/17/01 00:50:38
#
# Temporary command file to enable RSCT HA subsystems 
# with the manually edited 'subsys.conf' and 'topology.conf'
#
# Usage: 
#   ct_init_node [-c clname] [-n nodenum] topology.conf subsys.conf [nodelist]
#

PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin
RSCTBIN=/usr/sbin/rsct/bin
CTROOT=/var/ct

#
basecmd=$(basename $0)
#
function usage
{
CMDLINE="\
${basecmd} [-c clstname] [-n nodenumber] \
topology.conf subsys.conf [nodelist]"
    print -u2 "Usage: $CMDLINE"
}

# process the command line
CLSTNAME=
NODENUM=
while getopts ":c:n:" opt; do
    case $opt in
	n ) NODENUM=${OPTARG} ;;
        c ) CLSTNAME=${OPTARG} ;;
        * )
            print -u2 "${basecmd}: Not a recognized flag: -${OPTARG}"
            usage
            exit 1
            ;;
    esac
done

shift $(($OPTIND - 1))

if [[ -z $CLSTNAME ]]; then
   # use 'scaffold' as the cluster name
   CLSTNAME=scaffold
fi

if [[ -z $2  || -z $CLSTNAME ]]; then
   # no two arguments
   usage
   exit 1
fi

TOPOLOGYCONF=$1
SUBSYSCONF=$2
NODELIST=$3

if [[ ! -a $TOPOLOGYCONF ]]; then
    print -u2 "File $TOPOLOGYCONF not found"
    exit 1;
fi

if [[ ! -a $SUBSYSCONF ]]; then
   print -u2 "File $SUBSYSCONF not found"
   exit 1
fi

if [[ -z $NODENUM ]]; then
   # no node number is defined...find it from topology.conf
   # search this from $TOPOLOGYCONF
   ipaddr=$(/bin/hostname -i)
   # first check whether it is in the new format
   NODENUM=$(grep -i ADAPTER $TOPOLOGYCONF | grep -w $ipaddr | awk '{print $4}')
   if [[ -z $NODENUM ]]; then
   	# get the only entry starting with an integer
   	NODENUM=$(grep -w $ipaddr $TOPOLOGYCONF | \
		  grep '^[ \t]*[0-9]' | awk '{print $1}')
   fi
   if [[ -z $NODENUM  && -a ${NODELIST} ]]; then
        # not found in the topology.conf...search in the /var/ct/NODELIST
        NODENUM=$(grep -w $ipaddr $NODELIST | \
		  grep '^[ \t]*[0-9]'| awk '{print $1}')
        if [[ -z $NODENUM ]]; then
                hostname=$(/bin/hostname -f)
                NODENUM=$(grep $hostname $NODELIST | \
			  grep '^[ \t]*[0-9]'| awk '{print $1}')
        fi
   fi
   if [[ -z $NODENUM ]]; then
	if [[ -z ${NODELIST} ]]; then
           print -u2 "Node number not found in $TOPOLOGYCONF"
 	else
           print -u2 "Node number not found in $TOPOLOGYCONF or $NODELIST"
	fi
        exit 1
   fi

fi

# Only supported CLSTNAME is currently "IW"
CLSTDIR=/var/ct/${CLSTNAME}

clstdir_exist=0		# assume /var/ct/<CLST> not exist
if [[ -d $CLSTDIR ]]; then
    # clstdir already exist
    clstdir_exist=1

    mkdir -p ${CLSTDIR}/cfg/
    # copy SUBSYSCONF
    cp -f $SUBSYSCONF ${CLSTDIR}/cfg/subsys.conf

    # also copy topology.conf
    cp -f $TOPOLOGYCONF ${CLSTDIR}/cfg/topology.conf
fi

# SCAFFOLD:
# The followings will be used by 'ct_{hags|hats}_info' and 'ct_topology_info'
export CT_TOPOLOGY_CONF_FILE=$TOPOLOGYCONF
export CT_SUBSYS_CONF_FILE=$SUBSYSCONF

# initialize the cluster
$RSCTBIN/cthactrl -i -c $CLSTNAME -n $NODENUM -e scaffold
rc=$?

if [[ $clstdir_exist -eq 0 && -d $CLSTDIR ]]; then
   #clstdir was created by 'cthactrl -i' 
   mkdir -p ${CLSTDIR}/cfg/
   # copy SUBSYSCONF
   cp -f $SUBSYSCONF ${CLSTDIR}/cfg/subsys.conf

   # also copy topology.conf 
   cp -f $TOPOLOGYCONF ${CLSTDIR}/cfg/topology.conf
fi

exit $rc
